package com.atguigu.eduservice.service.impl;

import com.atguigu.baseservice.handler.exception.GuliException;
import com.atguigu.baseservice.result.ResultCodeEnum;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduCourseInfoForm;
import com.atguigu.eduservice.mapper.EduCourseDescriptionMapper;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.query.CourseQuery;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.eduservice.vo.CoursePublishVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Resource
    private EduCourseMapper eduCourseMapper;

    @Resource
    private EduCourseDescriptionMapper eduCourseDescriptionMapper;

    //新增课程基本信息
    @Override
    public String saveCourseInfo(EduCourseInfoForm eduCourseInfoForm) {

        //保存课程基本信息
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus(EduCourse.COURSE_DRAFT);
        BeanUtils.copyProperties(eduCourseInfoForm, eduCourse);
        boolean resultCourseInfo = this.save(eduCourse);
        if (!resultCourseInfo) {
            throw new GuliException(ResultCodeEnum.ADD_ERROR);
        }

        //保存课程详情信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(eduCourseInfoForm.getDescription());
        eduCourseDescription.setId(eduCourse.getId());
        boolean resultCourseDescription = eduCourseDescriptionService.save(eduCourseDescription);
        if (!resultCourseDescription) {
            throw new GuliException(ResultCodeEnum.ADD_ERROR);
        }

        return eduCourse.getId();
    }

    //根据id获取课程信息
    @Override
    public EduCourseInfoForm getCourseInfoFormById(String id) {

        //获取课程信息
        EduCourse educourse = eduCourseMapper.getEduCourseById(id);

        if (null == educourse) {
            throw new GuliException(ResultCodeEnum.NOT_FOUND);
        }

        //拷贝对象数据
        EduCourseInfoForm eduCourseInfoForm = new EduCourseInfoForm();
        BeanUtils.copyProperties(educourse, eduCourseInfoForm);

        //获取课程简介信息
        EduCourseDescription eduCourseDescription = eduCourseDescriptionMapper.getEduCourseDescriptionById(id);

        if (null != eduCourseDescription) {
            //填充简介信息
            eduCourseInfoForm.setDescription(eduCourseDescription.getDescription());
        }

        return eduCourseInfoForm;
    }

    //根据id修改课程信息
    @Override
    public void updateCourseInfoById(EduCourseInfoForm eduCourseInfoForm) {

        //修改课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(eduCourseInfoForm, eduCourse);
        boolean resultCourseInfo = this.updateById(eduCourse);
        if (!resultCourseInfo) {
            throw new GuliException(ResultCodeEnum.SAVE_ERROR);
        }

        //修改课程简介信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(eduCourseInfoForm.getDescription());
        eduCourseDescription.setId(eduCourseInfoForm.getId());
        boolean resultCourseDescription = eduCourseDescriptionService.updateById(eduCourseDescription);
        if (!resultCourseDescription) {
            throw new GuliException(ResultCodeEnum.SAVE_ERROR);
        }

    }

    // 查询课程发布信息
    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        return baseMapper.selectCoursePublishVoById(id);
    }

    // 发布课程
    @Override
    public boolean publishCourseById(String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus(EduCourse.COURSE_NORMAL);
        Integer result = baseMapper.updateById(eduCourse);
        return result > 0;
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        long page = pageParam.getCurrent();
        long pageSize = pageParam.getSize();

        if (page == 1) {
            page = 0;
        }

        List<EduCourse> eduCourseList = eduCourseMapper.pageQuery(page, pageSize, courseQuery);
        pageParam.setRecords(eduCourseList);
    }

    @Override
    public void pageQuery2(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");

        if (courseQuery == null) {
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();

        if(!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(teacherId)) {
            queryWrapper.eq("teacher_id", teacherId);
        }

        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.eq("subject_parent_id", subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.eq("subject_id", subjectId);
        }

        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public boolean removeCourseById(String id) {
        //如果用户确定删除，则首先删除video记录，然后删除chapter记录，最后删除Course记录
        boolean result1 = eduVideoService.removeByCourseId(id);

        boolean result2 = eduChapterService.removeByCourseId(id);

        Integer result3 = baseMapper.deleteById(id);

        return result1 && result2 && result3 > 0;
    }
}
